//
//  FQDSearchResultProvider.m
//  FakeQDaily
//
//  Created by mewxiv on 10/1/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDSearchResultProvider.h"
#import "FQDRestModel.h"
#import "QDailyAPI.h"

#import "YYModel.h"

@interface FQDSearchResultProvider ()

@property (nonatomic, strong) NSArray *results;

@end

@implementation FQDSearchResultProvider

- (void)getCellItems:(NSString *)cursorKey completion:(QDailyAPICompleteBlock)completion
{
    [[QDailyAPI sharedAPI] searchArticle:self.keyWord key:cursorKey completion:completion];
}

- (void)parseResponse:(id)response isLatest:(BOOL)isLatest
{
    if (isLatest) {
        self.totalNumber = [response[@"total_number"] integerValue];
    }
    
    self.results = [NSArray yy_modelArrayWithClass:[FQDSearchResult class]
                                              json:response[@"searches"]];
}

- (void)populateNewCellItemsTo:(NSMutableArray *)originalCellItems
{
    [originalCellItems addObjectsFromArray:self.results];
}

- (BOOL)hasMoreCellItems
{
    BOOL hasMoreCellItems = [super hasMoreCellItems];
    BOOL hasAll = ([self.cellItems count] <= self.totalNumber);
    return hasMoreCellItems && hasAll;
}

@end
